package com.base.cn.platform.os.service.marketing.baseShare;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.marketing.baseShare.MarketingBaseShareDao;
import com.base.cn.platform.os.entity.marketing.baseShare.MarketingBaseShare;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * 海报分享Biz
 *
 * @author zsw
 * @since 2019-04-18
 */
@Service
public class MarketingBaseShareBiz extends BaseBiz<MarketingBaseShare,MarketingBaseShareDao> {
    private Gson gson = new Gson();
    /**
     * 保存海报分享数据
     *
     * @param marketingBaseShare 保存海报对象
     * @return
     */
    public MarketingBaseShare saveBaseShare(MarketingBaseShare marketingBaseShare){
        if(DataUtil.idIsNotNull(marketingBaseShare.getId())){//修改
            this.updateById(marketingBaseShare,null);
        }else{//添加
            this.save(marketingBaseShare);
        }
        return marketingBaseShare;
    }

    /**
     * 查询分享数据
     * @return MarketingBaseShare
     */
    public MarketingBaseShare queryBaseShare() {
        String whereSql = " 1=1";
        MarketingBaseShare marketingBaseShare = this.findOne(whereSql,null);
        List<Map<String,String>> list = gson.fromJson(marketingBaseShare.getUrlMap(), List.class);
        Collections.sort(list, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                int a1 = StringUtils.letterToNumber(o1.get("selected"));
                int a2 = StringUtils.letterToNumber(o2.get("selected"));
                if(a1==a2){
                    return 0;
                }
                return a1 < a2 ? 1 : -1;
            }
        });
        marketingBaseShare.setImgObjList(list);
        return marketingBaseShare;
    }

}
